package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result<PageResult> page(DishPageQueryDTO dto) {
        if (dto.getPage() == 0) {
            dto.setPage(1);
        }
        if (dto.getPageSize() == 0) {
            dto.setPageSize(10);

        }
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<DishVO> page = dishMapper.page(dto);
        for (DishVO dishVO : page) {
            Category byId = categoryMapper.getById(dishVO.getCategoryId());
            if (byId != null) {
                dishVO.setCategoryName(byId.getName());
            }

        }

        return Result.success(new PageResult(page.getTotal(), page.getResult()));
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public Result getById(Long id) {

        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        DishVO dishVO = dishMapper.getById(id);
        dishVO.setFlavors(flavors);
        return Result.success(dishVO);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Transactional//开启事务
    @Override
    public Result upadteDish(DishDTO dto) {
        if (dto.getId() == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.updateDish(dish);
        //删除菜品关联的口味
        dishFlavorMapper.deleteByDishId(dto.getId());
        List<DishFlavor> flavors = dto.getFlavors();
        //把当前菜品的id赋值给口味表
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
            //新增口味表
            dishFlavorMapper.addDishFlavor(flavors);
        }


        return Result.success();
    }

    /**
     * 新增菜品
     *
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public Result addDish(DishDTO dto) {
        if (dto.getName() == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        String name = dto.getName();
        Dish dishMapperByName = dishMapper.getByName(name);
        if (dishMapperByName != null) {
            return Result.error(MessageConstant.DISHNAME_EXIT_FOUND);
        }

        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.addDish(dish);
        List<DishFlavor> flavors = dto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
        }
        dishFlavorMapper.addDishFlavor(flavors);

        return Result.success();
    }

    /**
     * 启售停售
     *
     * @param status
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result statusDish(Integer status, Long id) {
        if (status == null || id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateDish(dish);
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public Result delete(List<Long> ids) {
        if (ids == null || ids.size() == 0) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //判断菜品是否存在
        for (Long id : ids) {
            DishVO dishVO = dishMapper.getById(id);
            //如果存在且为停售
            if (dishVO != null && dishVO.getStatus() == 0) {
                List<Long> setmealDishIds = setmealDishMapper.getByDishId(dishVO.getId());
                //
                if (setmealDishIds.size() > 0 && setmealDishIds != null) {
                    return Result.error(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
                }
                dishMapper.deleteById(dishVO.getId());
                if (dishVO.getFlavors() != null && dishVO.getFlavors().size() > 0) {
                    dishFlavorMapper.deleteByDishId(dishVO.getId());
                }
            } else {
                return Result.error(MessageConstant.DISH_ON_SALE);
            }
        }

        return Result.success();
    }

    /**
     * 分类id查询菜品
     *
     * @param
     * @param
     * @return
     */
    @Override
    public Result getBycategoryId(DishDTO dto) {
        List<Dish> byCategoryId = dishMapper.getByCategoryIdAndName(dto);
        return Result.success(byCategoryId);
    }



    /*-------------------------------c端--------------------------------*/

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
